package com.zygh.policeasset.service;

import cn.hutool.core.date.DateUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zygh.policeasset.component.CommonResult;
import com.zygh.policeasset.component.SealOnOrderCode;
import com.zygh.policeasset.mapper.AuditFlowDetailMapper;
import com.zygh.policeasset.mapper.AuditFlowMapper;
import com.zygh.policeasset.mapper.EachTypeImagesMapper;
import com.zygh.policeasset.mapper.MaintenanceMapper;
import com.zygh.policeasset.mapper.RepairMapper;
import com.zygh.policeasset.pojo.AuditFlowDetail;
import com.zygh.policeasset.pojo.OrderReply;
import com.zygh.policeasset.pojo.Repair;
import com.zygh.policeasset.pojo.SealOnOrder;
import com.zygh.policeasset.pojo.TableType;
import com.zygh.policeasset.pojo.UpoladEachTypeImages;
import com.zygh.policeasset.pojo.vo.RepairVO;
import com.zygh.policeasset.utils.AuditUtil;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.util.StringUtils;

@Service
/* loaded from: police-asset-2.0-0.0.1-SNAPSHOT.jar:BOOT-INF/classes/com/zygh/policeasset/service/RepairService.class */
public class RepairService {
    @Autowired
    RepairMapper repairMapper;
    @Autowired
    AuditFlowMapper auditFlowMapper;
    @Autowired
    EachTypeImagesMapper eachTypeImagesMapper;
    @Autowired
    MaintenanceMapper maintenanceMapper;
    @Autowired
    AuditFlowService auditFlowService;
    @Autowired
    AuditFlowDetailMapper auditFlowDetailMapper;

    @Autowired
    SealOnOrderCode sealOnOrderCode;

    public RepairVO queryById(Integer id) {
        return this.repairMapper.queryById(id);
    }

    public PageInfo queryAll(int pageNum, int pageSize, Integer userId, Integer status, Integer orderStatus, Integer orderUserId) {
        PageHelper.startPage(pageNum, pageSize);
        return new PageInfo(this.repairMapper.queryAll(status, userId, orderStatus, orderUserId));
    }

    public List<RepairVO> queryByOrderUserId(Integer userId) {
        return this.repairMapper.selectByOrderUserId(null, userId);
    }

    public List<RepairVO> queryByIds(String ids) {
        return this.repairMapper.queryByIds(ids.split(","));
    }

    public PageInfo appQueryOrder(int pageNum, int pageSize, Integer userId, Integer status, Integer orderStatus, Integer orderUserId) {
        PageHelper.startPage(pageNum, pageSize);
        return new PageInfo(this.repairMapper.appQueryOrder(status, userId, orderStatus, orderUserId));
    }

    public CommonResult insert(Repair repair) {
        String now = DateUtil.now();
        String flowNo = AuditUtil.getFlowNo(now);
        repair.setFlowNo(flowNo);
        repair.setCreateTime(now);
        repair.setOrderUserId(repair.getUserId());
        int count = this.repairMapper.insert(repair);
        List<UpoladEachTypeImages> list = repair.getUploadFileList();
        if (list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
                if (!StringUtils.isEmpty(list.get(i).getContent())) {
                    this.eachTypeImagesMapper.add(repair.getId(), list.get(i).getContent(), now, "repair-fault");
                }
            }
        }
        this.auditFlowService.add(repair.getUserId(), flowNo, now, TableType.REPAIR.getName(), TableType.REPAIR.getType(), null, null);

        if (count > 0) {
            return new CommonResult().success();
        }
        return new CommonResult().validateFailed("添加失败");
    }

    public CommonResult update(Repair repair) {
        repair.setUpdateTime(DateUtil.now());
        if (this.repairMapper.update(repair) > 0) {
            return new CommonResult().success();
        }
        return new CommonResult().validateFailed("修改失败");
    }

    public CommonResult deleteById(Integer id) {
        if (this.repairMapper.deleteById(id) > 0) {
            return new CommonResult().success();
        }
        return new CommonResult().validateFailed("删除失败");
    }

    public CommonResult issuedOrder(Integer id, Integer userId) {
        if (this.repairMapper.issuedOrder(id, userId, 1, null, null) > 0) {
            return new CommonResult().success();
        }
        return new CommonResult().validateFailed("下发工单失败");
    }

    public CommonResult orderOperation(Integer id) {
        if (this.repairMapper.issuedOrder(id, null, 2, null, null) > 0) {
            return new CommonResult().success();
        }
        return new CommonResult().validateFailed("接收工单失败");
    }

    @Transactional(rollbackFor = {Exception.class})
    public CommonResult orderReply(OrderReply orderReply) {
        try {
            this.repairMapper.issuedOrder(orderReply.getId(), null, 3, DateUtil.now(), orderReply.getMaintenanceRemark());
            List<UpoladEachTypeImages> list = orderReply.getUploadFileList();
            if (list.size() > 0) {
                for (int i = 0; i < list.size(); i++) {
                    if (!StringUtils.isEmpty(list.get(i).getContent())) {
                        this.eachTypeImagesMapper.add(orderReply.getId(), list.get(i).getContent(), DateUtil.now(), "repair-maintenance");
                    }
                }
            }
            return new CommonResult().success();
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            e.printStackTrace();
            return new CommonResult().validateFailed(e.toString());
        }
    }

    /* JADX WARN: Type inference failed for: r0v100, types: [double] */
    public CommonResult sealOnOrder(String[] ids) {
        try {
            List<RepairVO> list = this.repairMapper.findByIds(ids);
            if (list.size() <= 0) {
                return new CommonResult().validateFailed("未查询到数据");
            }
            JSONObject json = new JSONObject();
            List<SealOnOrder> taskSources = new ArrayList<>();
            List<SealOnOrder> roadCrosss = new ArrayList<>();
            List<SealOnOrder> contents = new ArrayList<>();
            List<SealOnOrder> planCommenceTimes = new ArrayList<>();
            List<SealOnOrder> requiredCompletionTimes = new ArrayList<>();
            List<String> strs = new ArrayList<>();
            char c = 0;
            for (int i = 0; i < list.size(); i++) {
                if (list.get(i).getOrderStatus().intValue() != 3) {
                    return new CommonResult().validateFailed("选中工单中的工单状态不全部是已完成，请核对");
                }
                SealOnOrder taskSource = new SealOnOrder();
                taskSource.setSort(Integer.valueOf(i + 1));
                taskSource.setName(list.get(i).getTaskSourceName());
                taskSources.add(taskSource);
                SealOnOrder roadCross = new SealOnOrder();
                roadCross.setSort(Integer.valueOf(i + 1));
                roadCross.setName(list.get(i).getRoadCrossName());
                roadCrosss.add(roadCross);
                SealOnOrder content = new SealOnOrder();
                content.setSort(Integer.valueOf(i + 1));
                content.setName(list.get(i).getContent());
                contents.add(content);
                SealOnOrder planCommenceTime = new SealOnOrder();
                planCommenceTime.setSort(Integer.valueOf(i + 1));
                planCommenceTime.setName(list.get(i).getPlanCommenceTime());
                planCommenceTimes.add(planCommenceTime);
                SealOnOrder requiredCompletionTime = new SealOnOrder();
                requiredCompletionTime.setSort(Integer.valueOf(i + 1));
                requiredCompletionTime.setName(list.get(i).getRequiredCompletionTime());
                requiredCompletionTimes.add(requiredCompletionTime);
                c += list.get(i).getBudgetAmount();
                strs.add(list.get(i).getOrderUserName());
            }
            if (strs.size() > 1 && ((List) strs.stream().distinct().collect(Collectors.toList())).size() != 1) {
                return new CommonResult().validateFailed("选择中的工单中不属于与同一个维修单位，请核对");
            }
            AuditFlowDetail auditFlowDetail = new AuditFlowDetail();
            auditFlowDetail.setFlowNo(list.get(0).getFlowNo());
            List<AuditFlowDetail> auditFlowDetails = this.auditFlowDetailMapper.queryAll(auditFlowDetail);
            auditFlowDetails.remove(auditFlowDetails.get(0));
            json.put("approver", auditFlowDetails);
            json.put("sum", Double.valueOf(c));
            json.put("type", list.get(0).getType());
            json.put("taskSource", taskSources);
            json.put("roadCross", roadCrosss);
            json.put("content", contents);
            json.put("planCommenceTime", planCommenceTimes);
            json.put("requiredCompletionTime", requiredCompletionTimes);
            json.put("sealOrderNo", sealOnOrderCode.getCode(list.get(0).getType()));
            Optional<SealOnOrder> max = planCommenceTimes.stream().max(Comparator.comparing((v0) -> {
                return v0.getName();
            }));
            json.put("minTime", planCommenceTimes.stream().min(Comparator.comparing((v0) -> {
                return v0.getName();
            })).get().getName());
            json.put("maxTime", max.get().getName());
            json.put("orderUserName", strs.get(0));
            return new CommonResult().success(json);
        } catch (Exception e) {
            e.printStackTrace();
            return new CommonResult().validateFailed(e.toString());
        }
    }
}